package easy;

import org.junit.Test;

public class a1370_上升下降字符串 {
    public String sortString(String s) {
        int[] nums = new int[26];
        for(int i=0;i<s.length();i++){
            nums[s.charAt(i)-'a']++;
        }

//        StringBuffer拼接字符串，线程安全的,StringBuilder是不安全的
        StringBuffer buffer = new StringBuffer(s.length());
        while (buffer.length()<s.length()){
            for(int i=0;i<26;i++){
                if(nums[i]!=0){
                    buffer.append((char)(i+'a'));
                    nums[i]--;
                }
            }
            for(int i=25;i>=0;i--){
                if(nums[i]!=0){
                    buffer.append((char)(i+'a'));
                    nums[i]--;
                }
            }
        }
        return buffer.toString();
    }
    @Test
    public void test(){
        String s="aaabbbcccdddeeee";
        String s1 = tosort(s);
        System.out.println(s1);
    }
    public String tosort(String s){
        if(s==null){
            return "";
        }
        int[] nums = new int[26];
        for(int i=0;i<s.length();i++){
            nums[s.charAt(i)-'a']++;
        }
        StringBuffer buffer = new StringBuffer();
        while (buffer.length()<s.length()){
            for(int i=0;i<26;i++){
                if(nums[i]>0){
                    buffer.append((char)(i+'a'));
                    nums[i]--;
                }
            }
            for(int i=25;i>=0;i--){
                if(nums[i]>0){
                    buffer.append((char)(i+'a'));
                    nums[i]--;
                }
            }
        }
        return buffer.toString();
    }
}
